package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.HierCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.hier.HierBrowse;
import com.engineering.elink.domain.hier.PlannHier;
import com.engineering.elink.hierdao.HierBrowseMapper;
import com.engineering.elink.hierdao.HierStepMapper;
import com.engineering.elink.hierdao.HierTitleMapper;
import com.engineering.elink.hierdao.PlannHierMapper;
import com.engineering.elink.service.PlannHierService;
import com.engineering.elink.skilldao.SkillFileMapper;
import com.engineering.elink.skilldao.SkillMapper;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.CourseMapper;
import com.engineering.elink.vo.PlannHierCountVo;

@Service
public class PlannHierServiceImpl implements PlannHierService{
	
	@Autowired
	private PlannHierMapper plannHierMapper;
	
	@Autowired
	private HierTitleMapper hierTitleMapper;
	
	@Autowired
	private HierBrowseMapper hierBrowseMapper;
	
	@Autowired
	private HierStepMapper hierStepMapper;
	
	@Autowired
	private SkillMapper skillMapper;
	
	@Autowired
	private CourseMapper courseMapper;
	
	@Autowired
	private SkillFileMapper skillFileMapper;

	@Override
	public Map<String, Object> getPlannHiersList(BaseCo co) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<PlannHierCountVo> list = plannHierMapper.selectList(co);
		
		map.put("list", list);
		map.put("Pagination", new Pagination(co, plannHierMapper.selectCount()));
		//体系标题默认取为1的
		map.put("hierTitle", hierTitleMapper.selectByPrimaryKey(1));
	
		return map;
	}

	@Override
	public Map<String, Object> get(Integer hierId, String userId) {
		PlannHierCountVo vo = plannHierMapper.selectInfo(hierId);
		if (vo != null) {

			// 往体系浏览表插入数据
			hierBrowseMapper.insertSelective(new HierBrowse(hierId,userId));
			
			//更新体系浏览次数
			PlannHier ph=plannHierMapper.selectByPrimaryKey(hierId);
			ph.setVisitCount(ph.getVisitCount()+1);
			plannHierMapper.updateByPrimaryKeySelective(ph);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("plannHier", vo);
			//体系进阶信息
			map.put("hierStepList", hierStepMapper.getStepList("1,2"));
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS, map);

		} else {
			return ResJsonUtil.getMap(ErrorType.PLANNHIER_ERROR);
		}
	}

	@Override
	public Map<String, Object> getSkill(HierCo co,String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", skillMapper.getHierSkill(co));
		map.put("Pagination", new Pagination(co, skillMapper.selectHierSkillCount(co)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}
	
	@Override
	public Map<String, Object> getCourse(HierCo co,String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", courseMapper.getHierCourse(co));
		map.put("Pagination", new Pagination(co, courseMapper.selectHierCourseCount(co)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}
	
	@Override
	public Map<String, Object> getFile(HierCo co,String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", plannHierMapper.selectHierFile(co));
		map.put("Pagination", new Pagination(co, plannHierMapper.selectHierFileCount(co)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getCareerPlann(BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		//BaseCo co=new BaseCo();
		//co.setAll(true);
		//co.setPageSize(count);
		
		map.put("list", plannHierMapper.selectCareerPlann(co));
		map.put("Pagination", new Pagination(co, plannHierMapper.countCareerPlann(co)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

}
